Skip to content

fix: mutable default arg in Memory and improve robustness - #1

Open
KeloYuan wants to merge 1 commit into
Jacob-liu1996:mainfrom
KeloYuan:fix/mutable-default-and-improvements
Open

fix: mutable default arg in Memory and improve robustness#1
KeloYuan wants to merge 1 commit into
Jacob-liu1996:mainfrom
KeloYuan:fix/mutable-default-and-improvements

Conversation

@KeloYuan

@KeloYuan KeloYuan commented May 8, 2026

Copy link
Copy Markdown

Summary

This PR fixes a critical bug and adds several robustness improvements:

🐛 Critical Bug Fix

  • schema.py: Memory.messages used a mutable default argument (messages: List[Message] = []). In Python, mutable defaults are shared across all instances of a class — meaning multiple Memory() instances would share the same message list, causing cross-contamination between agents. Fixed by using Field(default_factory=list).

🔧 Robustness Improvements

  • agent.py: Add max_steps validation — MiniAgent(max_steps=0) or negative values now raise ValueError immediately instead of silently producing no output.
  • agent.py: Handle empty tool output gracefully — result_content[:100] on an empty string produced "" with trailing ..., now shows (empty) instead.

🔒 Security

  • tools.py: Add warnings.warn() when BashExecutor executes commands with shell=True, so users are aware of the security implications.

📦 Public API

  • __init__.py: Export Role and LLMResponse so users can import the full public API from mini_agent without reaching into submodules.

- Fix critical bug: Memory.messages used mutable default [] causing all
  instances to share the same list. Changed to Field(default_factory=list).
- Add max_steps validation in MiniAgent.__init__() to reject values < 1.
- Handle empty tool output gracefully in act() to avoid slicing empty string.
- Add security warning when BashExecutor executes with shell=True.
- Export Role and LLMResponse in __init__.py for full public API access.
Copilot AI review requested due to automatic review settings May 8, 2026 11:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a shared-state bug in the agent memory model and adds a few small safety/robustness improvements across the agent runtime and public API surface.

Changes:

  • Fix Memory.messages mutable default by switching to Field(default_factory=list).
  • Add max_steps validation and improve tool-output previewing for empty outputs.
  • Warn on BashExecutor usage of shell=True and export additional public API symbols (Role, LLMResponse) from mini_agent.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
mini_agent/tools.py Adds a security warning when executing shell commands via shell=True.
mini_agent/schema.py Fixes shared mutable default for Memory.messages using default_factory.
mini_agent/agent.py Validates max_steps and improves logging output for empty tool results.
mini_agent/init.py Exposes Role and LLMResponse in the package public API.
Comments suppressed due to low confidence (1)

mini_agent/tools.py:156

  • BashExecutor.execute is declared async but uses blocking subprocess.run(...), which will block the event loop and can stall concurrent agent steps. Consider running the subprocess via asyncio.create_subprocess_shell/exec or offloading the blocking call with asyncio.to_thread to keep the async API non-blocking.
            result = subprocess.run(
                command,
                shell=True,
                capture_output=True,
                text=True,
                timeout=30
            )

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a3504e58cb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread mini_agent/tools.py
Comment on lines +145 to +149
warnings.warn(
"BashExecutor uses shell=True which is a security risk. "
"Only use with trusted input.",
stacklevel=2,
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid failing BashExecutor under warnings-as-errors

Emitting warnings.warn(...) inside BashExecutor.execute can make every bash call fail in environments that run with PYTHONWARNINGS=error or -W error (common in CI/tests): warn raises UserWarning, it is caught by the broad except Exception, and the method returns success=False before/without running the command. This turns a diagnostic warning into a functional regression for those runtimes.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants